package v1

import (
	"blog/model"
	"blog/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func AddCategory(c *gin.Context){
	var category model.ArticleCategory
	c.ShouldBindJSON(&category)
	code,msg := model.AddCategory(&category)
	utils.HttpResponse(c,http.StatusOK,code,msg,category)
}

func GetCategory(c *gin.Context){
	name := c.Query("name")
	code,msg,category := model.GetCategory(name)
	utils.HttpResponse(c,http.StatusOK,code,msg,category)
}
func GetCategorys(c *gin.Context){
	page,_ := strconv.Atoi(c.Query("page"))
	pagesize,_ := strconv.Atoi(c.Query("pagesize"))
	if page==0 {
		page = -1
	}
	if pagesize==0 {
		pagesize = -1
	}
	code,msg,categorys,total:= model.GetCategorys(page,pagesize)
	utils.HttpResponseTotal(c,http.StatusOK,code,msg,categorys,total)
}
func UpdateCategory(c *gin.Context){
	id,_ := strconv.Atoi(c.Query("id"))
	name := c.Query("name")
	code,msg := model.UpdateCategory(id,name)
	utils.HttpResponse(c,http.StatusOK,code,msg,"")
}
func DeleteCategory(c *gin.Context){
	id,_ := strconv.Atoi(c.Param("id"))
	code,msg := model.DeleteCategory(id)
	utils.HttpResponse(c,http.StatusOK,code,msg,"")
}